#include "swarmSocketClient.cpp"
#include "swarmSocketServer.h"
//#include <ncurses>
#include <cstdio>
#include <fstream>
#include <string>
#include <map>
#include <iostream>
#include <cctype>
#include <cstdlib>
using namespace std;
void robotWrite(string input, int b, map<int, swarmSocketClient> &clients, map<int, bool> &cons)
{
	if(b != 0)
	{
		//cout<< "something\n";
		if(cons[b])
		{
			cout<< clients[b].writeMessage(input.c_str(), input.size());
		}
		//cout<< "something\n";
	}
	else
	{
		for(int i = 1; i <= clients.size(); i++)
		{
			if(cons[i])
			{
				cout<< clients[i].writeMessage(input.c_str(), input.size());
			}
		} 
	}
}
int robotConnect(map<int, swarmSocketClient>& clients, map<int, string>& ips, map<int, string>& passes,
		map<int, bool>& cons, string& robotadrress)
{
	ifstream fin;
	int b, i = 1;
	string input;
	fin.open(robotadrress.c_str());
	if(fin.fail())
	{
		return -1;
	}
	while(!fin.eof())
	{
		fin >> input;
		if(fin.eof())
		{
			break;
		}
		ips[i] = input;
		ips[i] = ips[i].substr(ips[i].find("@")+1, ips[i].size() - ips[i].find("@")-1);
		fin >> passes[i];
		clients.insert(pair<int, swarmSocketClient>(i, swarmSocketClient(ips[i].c_str(), 5555)));
		b = clients[i].initclient();		
		cons[i] = (0 == b);
		i++;
	}
	fin.close();
	return 0;
}

void textInterface(map<int, swarmSocketClient>& clients, map<int, string>& ips, map<int, string>& passes,
		map<int, bool>& cons, string& robotadrress)
{
	int b;
	char c;
	string input;
	system("clear");
	while(1)
	{
		
		printf("Select robot or esc to quit\n");
		printf("robot 0 is all %i\n", (int) ips.size());
		for(int i = 1; i <= ips.size(); i++)
		{
			printf("robot %i is %s connected:%i\n", i, ips[i].c_str(), cons[i]);
		}
		cin >> input;
		if(!(atoi(input.c_str())==0 && input[0] != '0'))
		{
			b = atoi(input.c_str());
			if(b > ips.size() || b < 0)
			{
				cout << "invalid robot: please try again";
			}
			else
			{
				system("clear");
				input = "";
				c = '\n';
				while(c != 'c')
				{
					printf("Selected robot is %i\n", b);
					printf("Select command\n");
					printf("R red light on\t\tr red light off\n");
					printf("G green light on\tg green light off\n");
					printf("B blue light on\t\tb blue light off\n");
					printf("< for left\t\t> for right\n");
					printf("( counter-clockwise\t) clockwise\n");
					printf("{ left go\t\t } right go\n");
					printf("[ left stop\t\t] right go\n");
					printf("^ for foward\t\t_ for stop\n");
					printf("s for bump sensor data\n");
					printf("c for cancel\n");
					cin >> c;
					switch(c)
					{
						case 'R':
							input = "17";
							break;
						case 'r':
							input = "16";
							break;
						case 'G':
							input = "33";
							break;
						case 'g':
							input = "32";
							break;
						case 'B':
							input = "49";
							break;
						case 'b':
							input = "48";
							break;
						case '{':
							input = "129";
							break;
						case '[':
							input = "128";
							break;
						case '}':
							input = "65";
							break;
						case ']':
							input = "64";
							break;
						case '^':
							input = "193";
							break;
						case '_':
							input = "192";
							break;
						case '(':
							input = "197";
							break;
						case ')':
							input = "198";
							break;
						case '<':
						case '>':
							cout<< "input a number\n";
							cin >> input;
							
						break;
						case 'c':
							input = "c";
							break;
						default:
							system("clear");
							cout<< "invalid command: please try again\n";
							break;
					
					}
					if(input != "")
					{
						
						system("clear");
						input = c + input;
						robotWrite(input, b, clients, cons);
					}	
				}
			}
				
		}
		else
		{
			if(input[0] == 27)
			{
				cout<< "good bye\n";
				return ;
			}
			else
			{
				cout<< "invalid command: please try again\n";
			} 
		}
		
	}

}

int main()
{
	fstream fin;
	fin.open("swarmlist.txt");
	if(fin.fail())
	{
		perror("fail:");
		return 1;
	}
	map<int, string> ips, passes;
	map<int, swarmSocketClient> clients;
	cout<< "opened\n";
	//swarmSocketClient client("163.11.127.122", 5555);
	//client.initclient();
	map<int, bool> cons;
	int i = 1;
	int b = 0;
	char c;
	string input;
	//fin >> ips[i];
	while(!fin.eof())
	{
		fin >> input;
		if(fin.eof())
		{
			break;
		}
		ips[i] = input;
		ips[i] = ips[i].substr(ips[i].find("@")+1, ips[i].size() - ips[i].find("@")-1);
		//cout<< ips[i] << endl;
		fin >> passes[i];
		//cout<< i << endl;
		//cin>> i;
		
		clients.insert(pair<int, swarmSocketClient>(i, swarmSocketClient(ips[i].c_str(), 5555)));
		//cout<< i << endl;
		b = clients[i].initclient();		
		cons[i] = (0 == b);		
		//cout<< b << endl;
		i++;
		//fin >> ips[i];
	}
	system("clear");
	while(1)
	{
		
		printf("Select robot or esc to quit\n");
		printf("robot 0 is all %i\n", (int) ips.size());
		for(i = 1; i <= ips.size(); i++)
		{
			printf("robot %i is %s connected:%i\n", i, ips[i].c_str(), cons[i]);
		}
		cin >> input;
		if(!(atoi(input.c_str())==0 && input[0] != '0'))
		{
			b = atoi(input.c_str());
			if(b > ips.size() || b < 0)
			{
				cout << "invalid robot: please try again";
			}
			else
			{
				system("clear");
				input = "";
				c = '\n';
				while(c != 'c')
				{
					printf("Selected robot is %i\n", b);
					printf("Select command\n");
					printf("R red light on\t\tr red light off\n");
					printf("G green light on\tg green light off\n");
					printf("B blue light on\t\tb blue light off\n");
					printf("< for left\t\t> for right\n");
					printf("( counter-clockwise\t) clockwise\n");
					printf("{ left go\t\t } right go\n");
					printf("[ left stop\t\t] right go\n");
					printf("^ for foward\t\t_ for stop\n");
					printf("s for bump sensor data\n");
					printf("c for cancel\n");
					cin >> c;
					switch(c)
					{
						case 'R':
							input = "17";
							break;
						case 'r':
							input = "16";
							break;
						case 'G':
							input = "33";
							break;
						case 'g':
							input = "32";
							break;
						case 'B':
							input = "49";
							break;
						case 'b':
							input = "48";
							break;
						case '{':
							input = "129";
							break;
						case '[':
							input = "128";
							break;
						case '}':
							input = "65";
							break;
						case ']':
							input = "64";
							break;
						case '^':
							input = "193";
							break;
						case '_':
							input = "192";
							break;
						case '(':
							input = "197";
							break;
						case ')':
							input = "198";
							break;
						case '<':
						case '>':
							cout<< "input a number\n";
							cin >> input;
							
						break;
						case 'c':
							input = "c";
							break;
						default:
							system("clear");
							cout<< "invalid command: please try again\n";
							break;
					
					}
					if(input != "")
					{
						
						system("clear");
						input = c + input;
						robotWrite(input, b, clients, cons);
					}	
				}
				/*if(b != 0 && c != 'c')
				{
					cout<< "something\n";
					if(cons[b])
					{
						cout<< clients[b].writeMessage(input.c_str(), input.size());
					}
					cout<< "something\n";
				}
				else
				{
					for(i = 1; i <= ips.size(); i++)
					{
						if(cons[b])
						{
							cout<< clients[i].writeMessage(input.c_str(), input.size());
						}
					} 
				}*/
			}
				
		}
		else
		{
			if(input[0] == 27)
			{
				cout<< "good bye\n";
				return 0;
			}
			else
			{
				cout<< "invalid command: please try again\n";
			} 
		}
		
	}












	return 0;
}






